/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.min.js";
import "../../../unit/common/js/bootstrap_js/ckeditor.js";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.multi-select.js";
// import "../../../unit/common/js/bootstrap_js/chosen.jquery.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import "../../../unit/common/js/bootstrap_js/table-editable.js";
import "../../../unit/common/js/bootstrap_js/bootstrap-datetimepicker.js";
import "../../../unit/common/js/bootstrap_js/bootstrap-datetimepicker.zh-CN.js";
import "../../../unit/common/js/bootstrap_js/form-components.js";
jQuery(function () {
    App.init();
    var token = sessionStorage.getItem('token');
    if (token == null || token =='') {
        location.href = HOST_FRT+'/logins/login.html';
    }

    jQuery('.logOut').attr('href','../logins/login.html');
    var projId = getArgs().id;

//获取基本信息
    jQuery.ajax({
        url: HOST_ADDR+'/cpmp/bidProjOnMngForPchs/view/'+projId,
        type: 'get',
        // data: JSON.stringify(data),
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,'login_type':'ums'
        },
        success: function(resp){
            if (!resp.success) {
                alert(resp.msgCod+' '+resp.msgInf);
                return;
            }
            var projqotCntTyp;
            if (resp.data.bidProjOn.qotCntTyp == 0) {
                projqotCntTyp='非实时报价';
            // } else if (resp.data.bidProjOn.qotCntTyp == 1) {
            //     projqotCntTyp='多次报价';
            } else if (resp.data.bidProjOn.qotCntTyp == 2) {
                projqotCntTyp='实时报价';
            } else {
                projqotCntTyp='';
            }
            var currTyp;
            if (resp.data.bidProjOn.currTyp == 'USD') {
                currTyp='美元';
            } else if (resp.data.bidProjOn.currTyp == 'RMB') {
                currTyp='人民币';
            } else {
                currTyp='';
            }
            var dpstFlg;
            if (resp.data.bidProjOn.dpstFlg == 0) {
                dpstFlg='不需要';
            } else if (resp.data.bidProjOn.dpstFlg == 1) {
                dpstFlg='需要';
            } else {
                dpstFlg='';
            }
            var pubPriFlg;
            if (resp.data.bidProjOn.pubPriFlg == 0) {
                pubPriFlg='否';
            } else if (resp.data.bidProjOn.pubPriFlg == 1) {
                pubPriFlg='是';
            } else {
                pubPriFlg='';
            }
            var bidRngTyp;
            if (resp.data.bidProjOn.bidRngTyp == 0) {
                bidRngTyp='非定向招标';
            } else if (resp.data.bidProjOn.bidRngTyp == 1) {
                bidRngTyp='定向招标';
            } else {
                bidRngTyp='';
            }

            if (resp.data.bidProjOn.bidOpenTim != null&&resp.data.bidProjOn.bidOpenTim !='') {
                var bidOpenTim = (new Date(resp.data.bidProjOn.bidOpenTim)).Format("yyyy-MM-dd hh:mm:ss");
            } else {
                bidOpenTim = '';
            }
            if (resp.data.bidProjOn.bidEndTim != 'null'&&resp.data.bidProjOn.bidEndTim !='') {
                var bidEndTim = (new Date(resp.data.bidProjOn.bidEndTim)).Format("yyyy-MM-dd hh:mm:ss");
            } else {
                bidEndTim = '';
            }
            if (resp.data.bidProjOn.projMemo != null&&resp.data.bidProjOn.projMemo !='') {
                var projMemo = resp.data.bidProjOn.projMemo;
            } else {
                projMemo = '';
            }
            var projNbr;
            if (resp.data.bidProjOn.projNbr != null &&resp.data.bidProjOn.projNbr !='') {
                projNbr=resp.data.bidProjOn.projNbr;
            } else {
                projNbr = '';
            }
            // console.log(resp)
            jQuery('.basicInfo').append('<div class="portlet-body"><table class="table table-striped table-bordered"><tbody><tr>' +
                '<td width="16%">项目名称：</td><td colspan="2">'+resp.data.bidProjOn.projNam+'</td> ' +
                '<td width="16%">项目编号：</td> <td colspan="2">'+projNbr+'</td></tr> <tr><tr>' +
                '<td width="16%">物料品类：</td><td colspan="2">'+resp.data.bidProjOn.matTyp+'</td>' +
                '<td width="16%">报价次数类型：</td><td colspan="2"><select class="span12 qot2CntTyp" data-placeholder="选择报价类型" tabindex="1">' +
                '<option value="">选择报价类型</option><option value="0">非实时报价</option> ' +
                '<option value="2">实时报价</option></select></td></tr><tr>' +
                '<td width="16%">招标单位：</td><td colspan="2">'+resp.data.bidProjOn.bidDptNam+'</td>' +
                '<td width="16%">招标单位地址：</td><td colspan="2">'+resp.data.bidProjOn.bidDptAddr+'</td></tr><tr>' +
                '<tr><td width="16%">是否需要投标保证金：</td><td colspan="2">'+dpstFlg+'</td>'+
                '<td width="16%">是否需要公开招标价格：</td><td colspan="2">'+pubPriFlg+'</td></tr>'+
                '<tr><td width="16%">投标保证金（元）：</td><td colspan="2">'+resp.data.bidProjOn.bidDpst+'</td>' +
                '<td width="16%">币种：</td><td colspan="2">'+currTyp+'</td></tr>'+
                '<tr><td width="16%">联系人：</td><td colspan="2">'+resp.data.bidProjOn.ctct+'</td>' +
                '<td width="16%">联系电话：</td><td colspan="2">'+resp.data.bidProjOn.ctctTel+'</td></tr><tr>' +
                '<td width="16%">投标开标时间：</td><td colspan="2">'+bidOpenTim+'</td>' +
                '<td width="16%">二次报价截止时间：</td><td colspan="2"><input name="endTm" type="text" class="span9 datetimepicker qot2EndTimStr" readonly="readonly" value=""/></td></tr><tr>' +
                '<td>招标范围类型：</td><td colspan="2">'+bidRngTyp+'</td>' +
                '<td width="16%">指定招标供应商：</td><td colspan="2">'+resp.data.splrs+'</td></tr></tr><tr>' +
                '<td>备注信息：</td><td colspan="5">'+projMemo+'</td></tr></tbody></table></div>');
            var nowTime = new Date().Format("yyyy-MM-dd hh:mm:ss");
            jQuery('.qot2EndTimStr').val(nowTime);
            jQuery('.datetimepicker').datetimepicker({
                language: 'zh-CN',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                forceParse: 0,
                showMeridian: 1,
                format: 'yyyy-mm-dd hh:ii:ss'///
            });
        },
        error:function(err){
            console.log(err);
        }
    });

//获取供应商
    var sta = {
        "projId":projId,
        "bidDocSts":'01'
    };
    jQuery.ajax({
        url: HOST_ADDR+'/cpmp/bidProjOnMngForPchs/getTendSplrs',
        type: 'post',
        data: JSON.stringify(sta),
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,'login_type':'ums'
        },
        success: function(resp){
            if (!resp.success) {
                alert(resp.msgCod+' '+resp.msgInf);
                return;
            }
            // console.log(resp)

            for (var i = 0;i< resp.data.length;i++) {
                jQuery('.chooseSup').append('<label class="checkbox"><span><input type="checkbox" value="'+resp.data[i].splrId+'"></span>'+resp.data[i].splrNam+'</label>');
            }
        },
        error:function(err){
            console.log(err);
        }
    });

//提交供应商
    jQuery('.supplierOk').click(function () {
        var suppliers = '';
        var qot2CntTyp = jQuery('.qot2CntTyp').val();
        var qot2EndTimStr = jQuery('.qot2EndTimStr').val();
        for(var i=0; i<jQuery('.chooseSup input').length; i++){
            if(jQuery('.chooseSup input')[i].checked) suppliers+=jQuery('.chooseSup input')[i].value+',';
        }
        if (suppliers.length == 0) {
            alert('请选择项目二次竞价供应商！');
            return;
        }
        if (suppliers != '' && qot2CntTyp!='' && qot2EndTimStr!='') {
            var data = {
                "projId":projId,
                "splrIds":suppliers,
                "qot2CntTyp":qot2CntTyp,
                "qot2EndTimStr":qot2EndTimStr
            };
            console.log(data)

            jQuery.ajax({
                url: HOST_ADDR+'/cpmp/bidProjOnMngForPchs/startQot2',
                type: 'post',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType:"application/json",
        headers: {
            'access_token': token,'login_type':'ums'
        },
                success: function(resp){
                    if (!resp.success) {
                        alert(resp.msgCod+' '+resp.msgInf);
                        return;
                    }
                    console.log(resp)
                    alert('发起二次竞价成功！')
                    location.href = '/bid_online/proj_list.html';
                },
                error:function(err){
                    console.log(err);
                }
            });
        } else {
            alert('供应商、报价类型、截标时间不得为空');
        }

    });
});
